package com.controll;

import java.io.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pojo.Archive;
import com.pojo.Files;
import com.service.ArchiveService;
import com.service.FileService;

@Controller
public class FileControll {
	/**
	 * 调用service层方法
	 */
	FileService service_file = new FileService();
	ArchiveService service = new ArchiveService();

	@RequestMapping(value = "/{id}/upload.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 上传照片
	 * @param id
	 * @param file
	 * @param req
	 * @return
	 */
	public String upload(@PathVariable String id,
			@RequestParam("file") MultipartFile file, HttpServletRequest req) {
		System.out.println(id);
		if (file.isEmpty()) {
			System.out.println("未选择文件");
			return "ERROR_NO_FILE";
		}
		try {
			byte[] bytes = file.getBytes();
			String fileName = file.getOriginalFilename();
			String fileExtension = fileName
					.substring(fileName.lastIndexOf(".") + 1);
			String newFileName = String.valueOf(System.currentTimeMillis())
					+ "." + fileExtension;
			Archive arc = service.queryOne(id);

			String filePath = String.format("%s\\%s\\%s\\%s", req.getSession()
					.getServletContext().getRealPath("/"), "upload",
					arc.getArchive_id(), newFileName);
			System.out.println(filePath);

			File existsFile = new File(filePath);
			if (!existsFile.getParentFile().exists()) {
				if (!existsFile.getParentFile().mkdirs()) {
					System.out.println("创建目录失败");
					return "ERROR_ON_MKDIR";
				}
			}
			if (existsFile.exists()) {
				if (existsFile.delete()) {
					// System.out.println("删除原始文件：" + filePath);
				} else {
					System.out.println("删除文件失败：" + filePath);
				}
			}
			BufferedOutputStream stream = new BufferedOutputStream(
					new FileOutputStream(new File(filePath)));
			stream.write(bytes);
			stream.close();
			service_file.add_file(id, newFileName);
			return "OK";
		} catch (Exception e) {
			e.printStackTrace();
			return "ERROR";
		}
	}

	@RequestMapping(value = "/query_file.action", method = RequestMethod.GET)
	@ResponseBody
	/**
	 * 查询某个档案的照片
	 * @param id
	 * @return
	 */
	public List<Files> query_file(
			@RequestParam(value = "id", defaultValue = "") String id) {
		System.out.println(id);
		List<Files> a = service_file.query_file(id);
		return a;
	}

	@RequestMapping(value = "/query_del1.action", method = RequestMethod.GET)
	@ResponseBody
	/**
	 * 查询要删除照片
	 * @param id
	 * @return
	 */
	public List<Files> query_del1(
			@RequestParam(value = "id", defaultValue = "") String id) {
		List<Files> c = service_file.query_del1(id);
		return c;
	}

	@RequestMapping(value = "/del.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 删除照片
	 * @param id
	 * @param req
	 * @return
	 */
	public String del(@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletRequest req) {
		System.out.println(id);
		Map<String, Object> arc = service.query1(id);
		String filePath = String.format("%s\\%s\\%s\\%s", req.getSession()
				.getServletContext().getRealPath("/"), "upload",
				arc.get("arc_id"), arc.get("name"));
		// System.out.println( archive.get(0).getArchive_id());
		File existsFile = new File(filePath);
		System.out.println(filePath);
		existsFile.delete();
		service_file.del(id);
		return "OK";
	}
}
